mui.init();
var popup = document.getElementById('popup');
var data;
var self;
var finaldata = [];
var searchKey;

var start = 1;
var limit = 75; //75个点事10行  

mui.plusReady(function() {
	self = plus.webview.currentWebview();
	initData();
	initListener();
});

window.addEventListener("reload", function() {
	start = 1;
	finaldata.length = 0;
	initData();
})

/*
 * 初始化数据
 * 
 */

function initData() {
	if(start == 1) {
		//清空
		$("#shigongtu").html("");
	}
	plus.nativeUI.showWaiting();
	ajaxF1(GETSGTZ, {
			id: self.ID,
			zh: searchKey,
			start: start,
			limit: limit
		}, function(result) {
			console.log("===================" + JSON.stringify(result));
			if(result.successful) {
				data = result.resultValue;
				if(data.length === limit) {
					start++;
					if(mui("#div_addMore")[0].classList.contains("mui-hidden")) {
						mui("#div_addMore")[0].classList.remove("mui-hidden")
					}
				} else {
					if(!mui("#div_addMore")[0].classList.contains("mui-hidden")) {
						mui("#div_addMore")[0].classList.add("mui-hidden")
					}
				}
				if(data && data.length > 0) {
					jQuery(".gcmc").html(data[0].SGCMC);
					var zs = result.resultValue.length;
					var size = Math.ceil(zs / 15);
					for(var i = 0; i < size; i++) {
						var odd = '<li class="mui-table-view-cell">';
						var even = '<li class="mui-table-view-cell">';
						for(var j = 0; j < 15 && (i * 15 + j < zs); j++) {
							var current = i * 15 + j;
							var tttt = finaldata.length + i * 15 + j;
							if(j < 7) {
								even += '<span class="mycircle"  data-state="0" data-me="' + tttt + '" ';
							} else {
								odd += '<span class="mycircle"   data-state="0"   data-me="' + tttt + '"';
							}
							//给每个点 添加颜色
							if(data[current].ZT == '2') {
								if(j < 7) {
									even += ' style="background:#1ABC9C"></span>';
								} else {
									odd += ' style="background:#1ABC9C"></span>';
								}

							} else if(data[current].ZT == '1') {
								if(j < 7) {
									even += ' style="background:#3399FF"></span>';
								} else {
									odd += ' style="background:#3399FF"></span>';
								}
							} else {
								if(j < 7) {
									even += ' style="background:#FFCC66"></span>';
								} else {
									odd += ' style="background:#FFCC66"></span>';
								}
							}
						}
						odd += '</li>';
						even += '</li>';
						$("#shigongtu").append(even);
						$("#shigongtu").append(odd);
					}
					$("#shigongtu li:even").addClass("itemType1");
					$("#shigongtu li:odd").addClass("itemType2");
					finaldata = finaldata.concat(data);
					//模拟上颜色
					//					$("#shigongtu li span").each(function() {
					//						var i = parseInt($(this).attr("data-me"));
					//						if(data[i].ZT == '2') {
					//							$(this).css({
					//								"background": "#1ABC9C"
					//							}); //已完成
					//						} else if(data[i].ZT == '1') {
					//							$(this).css({
					//								"background": "#3399FF"
					//							}); //进行中
					//						} else {
					//							$(this).css({
					//								"background": "#FFCC66"
					//							}); //未完成
					//						}
					//					})

				}
				initL();
			}

			plus.nativeUI.closeWaiting();
		},
		function() {
			mui.toast("请求失败");
			plus.nativeUI.closeWaiting();

		});

	//跳转到视频监控模块

}

function popbox(temp, ljz, i, index, tempsc) {
	temp.popover({
		'placement': 'auto',
		'animation': false,
		'html': true,
		'data-trigger': "click",
		'container': $("body"),
		'content': function() {
			//	var sss='<div id="tck' + index + '" class="tck"><div class="xqitem">桩号: <span class="xqcs">' + data[i].ZH + '</span></div><div class="xqitem">施工开始时间: <span class="xqcs">' + data[i].SGKSSJ + '</span></div><div class="xqitem">施工完成时间: <span class="xqcs">' + data[i].SGJSSJ + '</span></div> <div class="xqitem" >总时长:<span class="xqcs">' + tempsc + '</span></div><div class="xqitem">实际桩长: <span class="xqcs sjzcdatas">' + data[i].SHIJIZC + '米</span> 设计桩长: <span class="xqcs">' + data[i].SHEJIZC + '米</span></div><div class="xqitem">实际水泥用量: <span class="xqcs sjsnyldata">' + data[i].SHIJIYL + 'kg</span></div>	<div class="xqitem">设计水泥用量: <span class="xqcs">' + data[i].SHEJIYL + 'kg</span></div></div>'
			var sss = '';
			if(finaldata[i].SHEJIZC && finaldata[i].SHIJIZC > finaldata[i].SHEJIZC) {
				sss += '<div id="tck' + index + '" class="tck"><div class="xqitem">桩号: <span class="xqcs">' + finaldata[i].ZH + '</span></div><div class="xqitem">施工开始时间: <span class="xqcs">' + finaldata[i].SGKSSJ + '</span></div><div class="xqitem">施工完成时间: <span class="xqcs">' + finaldata[i].SGJSSJ + '</span></div> <div class="xqitem" >总时长:<span class="xqcs">' + tempsc + '</span></div><div class="xqitem">实际桩长: <span class="xqcs sjzcdatas" style="color:red" >'
			} else {
				sss += '<div id="tck' + index + '" class="tck"><div class="xqitem">桩号: <span class="xqcs">' + finaldata[i].ZH + '</span></div><div class="xqitem">施工开始时间: <span class="xqcs">' + finaldata[i].SGKSSJ + '</span></div><div class="xqitem">施工完成时间: <span class="xqcs">' + finaldata[i].SGJSSJ + '</span></div> <div class="xqitem" >总时长:<span class="xqcs">' + tempsc + '</span></div><div class="xqitem">实际桩长: <span class="xqcs sjzcdatas" >'
			}

			if(ljz && ljz > 58 * finaldata[i].SHEJIZC) {
				sss += finaldata[i].SHIJIZC + '米</span> 设计桩长: <span class="xqcs">' + finaldata[i].SHEJIZC + '米</span></div><div class="xqitem">实际水泥用量: <span class="xqcs sjsnyldata"  style="color:red">' + ljz + 'kg</span></div>'

			} else {
				sss += finaldata[i].SHIJIZC + '米</span> 设计桩长: <span class="xqcs">' + finaldata[i].SHEJIZC + '米</span></div><div class="xqitem">实际水泥用量: <span class="xqcs sjsnyldata">' + ljz + 'kg</span></div>'
			}
			sss += '<div class="xqitem">设计水泥用量: <span class="xqcs">' + 58 * finaldata[i].SHEJIZC + 'kg</span></div></div>'
			return sss;
		}

	});
}

function initListener() {
	//桌面取消弹出框
	//	$(body).popover('destroy');
	$("#div_addMore").on('tap', function() {
		//加载更多
		initData();
	})
	$(".icon_back").on("tap", function() {
		mui.back();
	})
	//搜索功能
	mui("body").on("keyup", "#input_search", function(e) {
		if(e.keyCode == 13) {
			searchKey = this.value;
			start = 1;
			finaldata.length = 0;
			initData();
		}
	});
}

function initL() {
	// 给列表添加点击事件
	$("#shigongtu").off("click").on('click', 'li span', function(e) {
		e.stopPropagation(); //表示阻止向父元素冒泡
		e.preventDefault(); //
		var i = $(this).attr("data-me");
		var sss = $(this); //由谁弹出
		var index = $(this).index();
		var temp = $(this);
		var tempsc = getSjc(finaldata[i].SGKSSJ, finaldata[i].SGJSSJ);
		//	$("body").popover('destroy');
		if(finaldata[i].ZT == '2') {
			//请求ajax
			plus.nativeUI.showWaiting();
			var ajaxss = ajaxF1(GETSGLL, {
				jbzid: finaldata[i].ID,
				kssj: finaldata[i].SGKSSJ,
				jssj: finaldata[i].SGJSSJ
			}, function(resultss) {
				plus.nativeUI.closeWaiting();
				var ss = resultss.resultValue;
				//获取密码
				var shb = getSHB();
				if(ss[0].MIDU ==null){
					ss[0].MIDU=0;
				}
				var tmp = parseFloat(ss[0].MIDU).toFixed(2);
				var zsmd = shb[tmp];
				var zxz = parseFloat(Object.keys(shb)[0]);
				var zdz = parseFloat(Object.keys(shb)[Object.keys(shb).length - 1]);
				var lsz = parseFloat(ss[0].MIDU).toFixed(2);
				if(zsmd == null) {
					if(lsz < zxz) {
						zsmd = Object.keys(shb)[0];
					} else if(lsz > zdz) {
						zsmd = Object.keys(shb)[Object.keys(shb).length - 1]
					}
				}else{
					zsmd=tmp;
				}
				if(ss && ss.length > 0) {
					ljz = (1000 * (ss[0].ZDZ - ss[0].ZXZ) * parseFloat(shb[zsmd])).toFixed(2);
					if(isNaN(ljz)) {
						ljz = 0;
					}
				}
				popbox(temp, ljz, i, index, tempsc);
				if(temp.attr("data-state") == "0") {
					temp.attr("data-state", "1")
					temp.trigger("click");
				}
			}, function() {
				plus.nativeUI.closeWaiting();
			});
		} else if(finaldata[i].ZT == '1') {
			// 跳转页面
			jumpTo("../sgxqadmin/sgxqadmin.html", 'sgxqadmin.html', jumpOptionFadein, {
				SGCMC: finaldata[i].SGCMC,
				guid: finaldata[i].ID,
				SGCID: self.ID,
				ZH: finaldata[i].ZH,
				SJZC: finaldata[i].SHEJIZC || 0,
				"SJNJBZ": finaldata[i].SJNJBZ || 0
			})
		} else {
			mui.toast("该水泥柱未开始施工");
		}
	});
}